package com.baidu.videoseries.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.videoseries.R;
import com.baidu.videoseries.widget.PlayerShutter;
import com.kvin.toolkit.utils.LogUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TestFragment extends BaseFragment {


    PlayerShutter mShutter;
    Unbinder unbinder;
    private Timer mTimer;
    private int progress;

    public TestFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_play_detail, container, false);
        this.mShutter = (PlayerShutter) view.findViewById(R.id.m_shutter);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void init() {
        mShutter.setMaxProgress(100);
        this.mTimer = new Timer();
        this.mTimer.schedule(new LoadTask(), 0, 1000);
    }


    /**
     *
     */
    class LoadTask extends TimerTask {
        @Override
        public void run() {
            if (progress > mShutter.getMaxProgress()) return;
            mHandler.sendEmptyMessage(progress++);
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            LogUtils.v("---" + (mShutter == null)+"--pro="+mShutter.getProgress());
            mShutter.setProgress(msg.what);
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
